package br.unifesp.lma.chamada.dao;

import static com.googlecode.objectify.ObjectifyService.ofy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import br.unifesp.lma.chamada.model.Entidade;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.cmd.LoadType;

public abstract class BaseDAO<T extends Entidade> {

	protected Class<T> clazz;

	protected BaseDAO(Class<T> clazz) {
		this.clazz = clazz;
	}

	public LoadType<T> query() {
		return ofy().load().type(clazz);
	}

	public void put(T entity) {
		ofy().save().entity(entity).now();
	}

	public void put(List<T> entities) {
		ofy().save().entities(entities).now();
	}

	public void delete(T entity) {
		ofy().delete().entity(entity).now();
	}

	public List<T> get(Collection<Long> ids) {
		return new ArrayList<T>(ofy().load().type(clazz).ids(ids).values());
	}

	public T get(Long id) {
		Key<T> k = Key.create(clazz, id);
		return ofy().load().key(k).get();
	}

	public List<T> getAll() {
		return ofy().load().type(clazz).list();
	}
}